package com.unicloud.medical.oss.client.util;

import com.google.protobuf.ByteString;
import com.google.protobuf.ProtocolStringList;
import com.unicloud.medical.oss.common.enums.ErrorEnum;
import com.unicloud.medical.oss.common.model.BucketInfo;
import com.unicloud.medical.oss.common.model.DownloadObject;
import com.unicloud.medical.oss.common.model.ObjectInfo;
import com.unicloud.medical.oss.service.grpc.*;
import com.unisound.medical.common.model.dto.ResultDTO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class ResultDTOUtil {
    public static ResultDTO<Boolean> toResultDTO(GrpcResultDTO grpcResultDTO) {
        if (Objects.isNull(grpcResultDTO)) return ResultDTO.fail(ErrorEnum.SYSTEM_ERROR);
        return ResultDTO.result(grpcResultDTO.getCode(), grpcResultDTO.getMessage(), grpcResultDTO.getSuccess(), grpcResultDTO.getData());
    }

    public static ResultDTO<String> toResultDTO(GrpcResultDTOV2 grpcResultDTO) {
        if (Objects.isNull(grpcResultDTO)) return ResultDTO.fail(ErrorEnum.SYSTEM_ERROR);
        return ResultDTO.result(grpcResultDTO.getCode(), grpcResultDTO.getMessage(), grpcResultDTO.getSuccess(), grpcResultDTO.getData());
    }

    public static ResultDTO<List<String>> toResultDTO(GrpcResultDTOV3 grpcResultDTO) {
        if (Objects.isNull(grpcResultDTO)) return ResultDTO.fail(ErrorEnum.SYSTEM_ERROR);
        return ResultDTO.result(grpcResultDTO.getCode(), grpcResultDTO.getMessage(), grpcResultDTO.getSuccess(), toStringList(grpcResultDTO.getDataList()));
    }

    public static ResultDTO<List<ObjectInfo>> toResultDTO(GrpcResultDTOV4 grpcResultDTO) {
        if (Objects.isNull(grpcResultDTO)) return ResultDTO.fail(ErrorEnum.SYSTEM_ERROR);
        return ResultDTO.result(grpcResultDTO.getCode(), grpcResultDTO.getMessage(), grpcResultDTO.getSuccess(), toObjectInfoList(grpcResultDTO.getDataList()));
    }

    public static ResultDTO<List<BucketInfo>> toResultDTO(GrpcResultDTOV5 grpcResultDTO) {
        if (Objects.isNull(grpcResultDTO)) return ResultDTO.fail(ErrorEnum.SYSTEM_ERROR);
        return ResultDTO.result(grpcResultDTO.getCode(), grpcResultDTO.getMessage(), grpcResultDTO.getSuccess(), toBucketInfoList(grpcResultDTO.getDataList()));
    }

    public static ResultDTO<DownloadObject> toResultDTO(GrpcResultDTOV6 grpcResultDTO) {
        if (Objects.isNull(grpcResultDTO)) return ResultDTO.fail(ErrorEnum.SYSTEM_ERROR);

        GrpcDownloadObject data = grpcResultDTO.getData();
        return ResultDTO.result(grpcResultDTO.getCode(), grpcResultDTO.getMessage(), grpcResultDTO.getSuccess(), new DownloadObject(data.getContentType(), data.getFileName(), data.getData().toByteArray()));
    }

    private static List<String> toStringList(ProtocolStringList dataList) {
        if (Objects.isNull(dataList)) return null;
        List<ByteString> byteStrings = dataList.asByteStringList();
        if (CollectionUtils.isEmpty(byteStrings)) return null;
        return byteStrings.stream()
                .map(byteString -> byteString.toStringUtf8())
                .collect(Collectors.toList());
    }

    private static List<ObjectInfo> toObjectInfoList(List<GrpcObjectInfo> dataList) {
        if (CollectionUtils.isEmpty(dataList)) return null;
        return dataList.stream()
                .map(grpcObjectInfo -> new ObjectInfo(grpcObjectInfo.getName(), grpcObjectInfo.getSize()))
                .collect(Collectors.toList());
    }

    private static List<BucketInfo> toBucketInfoList(List<GrpcBucketInfo> dataList) {
        if (CollectionUtils.isEmpty(dataList)) return null;
        return dataList.stream()
                .map(grpcBucketInfo -> new BucketInfo(grpcBucketInfo.getName(), grpcBucketInfo.getCreateDate()))
                .collect(Collectors.toList());
    }

    private static DownloadObject toDownloadObject(GrpcDownloadObject grpcDownloadObject) {
        if (Objects.isNull(grpcDownloadObject)) return null;
        return new DownloadObject(grpcDownloadObject.getContentType(), grpcDownloadObject.getFileName(), grpcDownloadObject.getData().toByteArray());
    }

}
